package com.example.imtoken.modules.main.wallet.add_token

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClickUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseActivity
import com.example.imtoken.core.utils.InjectorUtils
import com.example.imtoken.databinding.ActivityAddTokenBinding
import com.example.imtoken.modules.main.MainActivity
import com.example.imtoken.modules.user.create.backups.BackupsActivity

/**
 * 添加代币
 */
class AddTokenActivity :
    BaseActivity<AddTokenViewModel, ActivityAddTokenBinding>(AddTokenViewModel::class.java) {

    private val addTokenType by lazy {
        intent.getIntExtra("addTokenType", 0)
    }

    private val viewModelF by lazy {
        getViewModelFactory(InjectorUtils.getAddTokenViewModel())
    }

    private lateinit var adapter: AddTokenAdapter

    override fun initLayoutId(): Int {
        return R.layout.activity_add_token
    }

    override fun init() {
        initTitle()
        initClick()
        initAdapter()
        viewModelF.addTokenType = addTokenType
        when (addTokenType) {
            1, 3 -> viewModelF.getAdminCoins()
            else -> viewModelF.getCoins()
        }
//        if (addTokenType == 1) {
//            viewModel.getAdminCoins()
//        } else {
//            viewModel.getCoins()
//        }
        initObserve()
    }

    private fun initAdapter() {
        adapter = AddTokenAdapter(R.layout.item_add_token)
        adapter.setDiffCallback(AddTokenDiffCallback())
        dataBinding.rvCoins.layoutManager = LinearLayoutManager(this)
        dataBinding.rvCoins.adapter = adapter
        adapter.setOnItemClickListener { _, _, position ->
            viewModelF.checkedCoin(position)
        }
    }

    private fun initObserve() {
        viewModelF.getCoinsData().observe(this) {
            adapter.setDiffNewData(it)
        }
        viewModelF.isSuccess.observe(this) {
            if (it) {
                when (addTokenType) {
                    0 -> ActivityUtils.startActivity(BackupsActivity::class.java)
                    1 -> {
                        finish()
                    }
                    2 -> ActivityUtils.startActivity(MainActivity::class.java)
                    3 -> {
                        setResult(200)
                        finish()
                    }
                }
            }
        }
    }

    private fun initTitle() {
        dataBinding.headView.tvHeadTitles.text = getString(R.string.addToken)
//        if (addTokenType == 1) {
//            dataBinding.headView.ivHeadBack.visibility = View.VISIBLE
//        } else {
//            dataBinding.headView.ivHeadBack.visibility = View.INVISIBLE
//        }
        when (addTokenType) {
            1, 3 -> dataBinding.headView.ivHeadBack.visibility = View.VISIBLE
            else -> dataBinding.headView.ivHeadBack.visibility = View.INVISIBLE
        }
    }

    private fun initClick() {
        ClickUtils.applyGlobalDebouncing(dataBinding.tvConfirm) {
            viewModelF.addWallet()
        }
        dataBinding.headView.ivHeadBack.setOnClickListener { this.finish() }
    }
}